const router = require('express').Router()
const {query} = require('../db')

// 获取角色列表
router.get('/role', async (req, res) => {
  const page = req.query.page || 1
  const pagesize = 10
  const offset = (page - 1) * pagesize
  const sql = `select * from roles order by id desc limit ${offset},${pagesize}`
  const data = await query(sql)

  res.send({
    code: 0,
    msg: 'ok',
    data
  })
})


// 添加角色
router.post('/role', async (req, res) => {
  const {name, showname} = req.body
  const sql = `insert into roles (name,showname) values ('${name}','${showname}')`
  const ret = await query(sql)
  res.send({
    code: 0,
    msg: 'ok',
    data: {name}
  })
})

module.exports = router
